<?php

namespace App\Http\Controllers\v1;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Http\v1\ResponseFactory;
use Illuminate\Support\Facades\Validator;
use App\Exceptions\v1\BadParam;
use App\Repositories\MeetingRoomRepository;

class MeetingRoomController extends Controller
{
    public function index(Request $request)
    {
        $v = Validator::make($request->all(), [
            'space_id',
            'page' => 'required',
            'amount' => 'required',
        ]);

        if ($v->fails()) {
            throw new BadParam($v->errors()->first());
        }

        $spaceId = $request->get('space_id');
        $page = $request->get('page');
        $amount = min($request->get('amount'), 40);

        $meetingRooms = MeetingRoomRepository::getList($spaceId, $page, $amount);

        return ResponseFactory::success($meetingRooms);
    }
}
